<?php
class DbCommand{
    public $raw;
    public $error;
    public $affected_rows;
    public $params;
    public $sql;
    function __construct($raw,$sql){
        $this->raw = $raw;
        $this->params=array();
    }
    function add_parameter($value,$type=null){
        if(!$type){
            if(is_string($value)) $type= "s";
            else if(is_double($value)) $type = "d";
            else if(is_int($value))$type="i";
            else if(!$value) $type="s";
            else $type = "b";
        }
        $this->params[] = array('type'=>$type,'value'=>$value);
        return $this;
    }

    function execute(){
        $f = "";
        $args = array();
        $args[] = &$f;
        foreach($this->params as $par){
            $f.=$par["type"];
            $args[] = $par["value"];
        }
        call_user_func_array(array($this->raw,"bind_param"),$args);
        $rs = $this->raw->execute();
        $this->affected_count = $this->raw->$affected_count;
        $this->error = $this->raw->error;
         return $rs;
    }

    
    function query($oop=false){
        $rs = $this->execute();
        
        if($rs){
            return $this->raw->get_result();
        }
    }
    function first(){
        $rs = $this->execute();
        
        if($rs){
            $results= $this->raw->get_result();
            if($results) return $results[0];
        }
    }

    function close(){
        $this->raw->close();
    }
}